var login = require('../../utils/login.js');
const utils = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logo: "",
    mallName: "",
    backUrl: "",
    back:"",
    backoptions:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
        back: options.back
    })
    if (options.query) {
      var query = JSON.parse(options.query);
      this.setData({
        backoptions: query
      })
      var s = "?"
      for (var key in query) {
        s += key + "=" + query[key] + "&";
      }
      s = s.substring(0, s.length - 1);
      var backUrl = options.back + s;
    } else {
      var backUrl = options.back;
    }
    var mallName = wx.getStorageSync("mallName");
    var logoSrc = wx.getStorageSync("logoSrc");
    if (logoSrc) {
      var logoSrc = 'https://static.711688.net.cn/upload/store/' + app.globalData.storeId + '/logo/source/' + logoSrc;
    } else {
      var logoSrc = "";
    }
    this.setData({
      logo: logoSrc,
      mallName: mallName,
      backUrl: "/" + backUrl
    })
    wx.setNavigationBarTitle({
      title: "微信授权登录"
    })
  },

  //注册
  onGotUserInfo: function (e) {
    var that = this;
    var iv = e.detail.iv;
    var encryptedData = e.detail.encryptedData;
    wx.login({
      success: function (res) {
        var code = res.code;
        // 下面开始调用注册接口
        wx.request({
          url: app.globalData.host + '/api/account/WxOpenRegister',
          data: { code: code, encryptedData: encryptedData, iv: iv, storeid: app.globalData.storeId, storeAppId: app.globalData.storeAppId }, // 设置请求的 参数
          success: (res) => {
            if (res.data.Code == 0) {
              login.login(app);
              var currUrl = that.data.backUrl;
              if (currUrl.indexOf("?") != -1) {
                currUrl = currUrl.split("?")[0];
              }
              if (that.data.back != "pages/index/index"){
                wx.redirectTo({
                  url: that.data.backUrl,
                })
              }else{
                if (JSON.stringify(that.data.backoptions) != "{}" && that.data.back == "pages/index/index"){
                  wx.redirectTo({
                    url: "/pages/index/index",
                  })
                }else{
                  wx.redirectTo({
                    url: "/pages/weijieke/cardList/index",
                  })
                }
              }
            } else {
              wx.showModal({
                title: '提示',
                content: "请在授权确认框点击允许按钮！",
                showCancel: false
              })
            }
          }
        })
      }
    })
  },
  
  //消息记录
  formSubmitAuthori:function(e){
    console.log(e)
    var formId = e.detail.formId;
    utils.addFormIdMsg(formId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
})